/*     */ package com.zimbra.cs.mailbox.calendar;
/*     */ 
/*     */ import com.zimbra.common.calendar.Attach;
/*     */ import com.zimbra.common.calendar.Geo;
/*     */ import com.zimbra.common.calendar.ICalTimeZone;
/*     */ import com.zimbra.common.calendar.TZIDMapper;
/*     */ import com.zimbra.common.calendar.TimeZoneMap;
/*     */ import com.zimbra.common.calendar.WellKnownTimeZones;
/*     */ import com.zimbra.common.calendar.ZCalendar.ICalTok;
/*     */ import com.zimbra.common.calendar.ZCalendar.ZParameter;
/*     */ import com.zimbra.common.calendar.ZCalendar.ZProperty;
/*     */ import com.zimbra.common.localconfig.DebugConfig;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.mailbox.Metadata;
/*     */ import java.util.ArrayList;
/*     */ import java.util.HashMap;
/*     */ import java.util.Iterator;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.Map.Entry;
/*     */ import java.util.Set;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class Util
/*     */ {
/*     */   private static final String FN_NAME = "n";
/*     */   private static final String FN_NUM_XPROPS_OR_XPARAMS = "numX";
/*     */   private static final String FN_VALUE = "v";
/*     */   private static final String FN_XPROP_OR_XPARAM = "x";
/*     */   private static final String FN_TZID = "tzid";
/*     */   private static final String FN_STANDARD_OFFSET = "so";
/*     */   private static final String FN_DAYLIGHT_OFFSET = "do";
/*     */   private static final String FN_DAYTOSTD_DTSTART = "d2ss";
/*     */   private static final String FN_STDTODAY_DTSTART = "s2ds";
/*     */   private static final String FN_DAYTOSTD_RULE = "d2sr";
/*     */   private static final String FN_STDTODAY_RULE = "s2dr";
/*     */   private static final String FN_STANDARD_TZNAME = "sn";
/*     */   private static final String FN_DAYLIGHT_TZNAME = "dn";
/*     */   private static final String FN_CONTENT_TYPE = "ct";
/*     */   private static final String FN_URI = "uri";
/*     */   private static final String FN_BINARY = "bin";
/*     */   private static final String FN_LATITUDE = "lat";
/*     */   private static final String FN_LONGITUDE = "lon";
/*     */   
/*     */   public static void encodeXParamsAsMetadata(Metadata meta, Iterator<ZCalendar.ZParameter> xparamsIter)
/*     */   {
/*  68 */     int xparamCount = 0;
/*  69 */     while (xparamsIter.hasNext()) {
/*  70 */       ZCalendar.ZParameter xparam = (ZCalendar.ZParameter)xparamsIter.next();
/*  71 */       String paramName = xparam.getName();
/*  72 */       if (paramName != null) {
/*  73 */         Metadata paramMeta = new Metadata();
/*  74 */         paramMeta.put("n", paramName);
/*  75 */         String paramValue = xparam.getValue();
/*  76 */         if (paramValue != null)
/*  77 */           paramMeta.put("v", paramValue);
/*  78 */         meta.put("x" + xparamCount, paramMeta);
/*  79 */         xparamCount++;
/*     */       } }
/*  81 */     if (xparamCount > 0)
/*  82 */       meta.put("numX", xparamCount);
/*     */   }
/*     */   
/*     */   public static void encodeXPropsAsMetadata(Metadata meta, Iterator<ZCalendar.ZProperty> xpropsIter) {
/*  86 */     int xpropCount = 0;
/*  87 */     while (xpropsIter.hasNext()) {
/*  88 */       ZCalendar.ZProperty xprop = (ZCalendar.ZProperty)xpropsIter.next();
/*  89 */       String propName = xprop.getName();
/*  90 */       if ((propName != null) && 
/*     */       
/*  92 */         (!propName.equalsIgnoreCase(ZCalendar.ICalTok.X_ZIMBRA_CHANGES.toString()))) {
/*  93 */         Metadata propMeta = new Metadata();
/*  94 */         propMeta.put("n", propName);
/*  95 */         String propValue = xprop.getValue();
/*  96 */         if (propValue != null) {
/*  97 */           propMeta.put("v", propValue);
/*     */         }
/*  99 */         encodeXParamsAsMetadata(propMeta, xprop.parameterIterator());
/*     */         
/* 101 */         meta.put("x" + xpropCount, propMeta);
/* 102 */         xpropCount++;
/*     */       } }
/* 104 */     if (xpropCount > 0)
/* 105 */       meta.put("numX", xpropCount);
/*     */   }
/*     */   
/*     */   public static List<ZCalendar.ZParameter> decodeXParamsFromMetadata(Metadata meta) throws ServiceException {
/* 109 */     int xparamCount = (int)meta.getLong("numX", 0L);
/* 110 */     if (xparamCount > 0) {
/* 111 */       List<ZCalendar.ZParameter> list = new ArrayList(xparamCount);
/* 112 */       for (int paramNum = 0; paramNum < xparamCount; paramNum++) {
/* 113 */         Metadata paramMeta = meta.getMap("x" + paramNum, true);
/* 114 */         if (paramMeta != null) {
/* 115 */           String paramName = paramMeta.get("n", null);
/* 116 */           if (paramName != null) {
/* 117 */             String paramValue = paramMeta.get("v", null);
/* 118 */             ZCalendar.ZParameter xparam = new ZCalendar.ZParameter(paramName, paramValue);
/* 119 */             list.add(xparam);
/*     */           } } }
/* 121 */       return list;
/*     */     }
/* 123 */     return null;
/*     */   }
/*     */   
/*     */   public static List<ZCalendar.ZProperty> decodeXPropsFromMetadata(Metadata meta) throws ServiceException {
/* 127 */     int xpropCount = (int)meta.getLong("numX", 0L);
/* 128 */     if (xpropCount > 0) {
/* 129 */       List<ZCalendar.ZProperty> list = new ArrayList(xpropCount);
/* 130 */       for (int propNum = 0; propNum < xpropCount; propNum++) {
/* 131 */         Metadata propMeta = meta.getMap("x" + propNum, true);
/* 132 */         if (propMeta != null) {
/* 133 */           String propName = propMeta.get("n", null);
/* 134 */           if (propName != null)
/*     */           {
/* 136 */             if (!propName.equalsIgnoreCase(ZCalendar.ICalTok.X_ZIMBRA_CHANGES.toString())) {
/* 137 */               ZCalendar.ZProperty xprop = new ZCalendar.ZProperty(propName);
/* 138 */               String propValue = propMeta.get("v", null);
/* 139 */               if (propValue != null)
/* 140 */                 xprop.setValue(propValue);
/* 141 */               List<ZCalendar.ZParameter> xparams = decodeXParamsFromMetadata(propMeta);
/* 142 */               if (xparams != null) {
/* 143 */                 for (ZCalendar.ZParameter xparam : xparams) {
/* 144 */                   xprop.addParameter(xparam);
/*     */                 }
/*     */               }
/* 147 */               list.add(xprop);
/*     */             } } } }
/* 149 */       return list;
/*     */     }
/* 151 */     return null;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public static ICalTimeZone getAccountTimeZone(Account account)
/*     */   {
/* 158 */     String tzid = account.getAttr("zimbraPrefTimeZoneId");
/* 159 */     tzid = TZIDMapper.canonicalize(tzid);
/* 160 */     ICalTimeZone timeZone = WellKnownTimeZones.getTimeZoneById(tzid);
/* 161 */     if (timeZone == null) {
/* 162 */       return ICalTimeZone.getUTC();
/*     */     }
/* 164 */     return timeZone;
/*     */   }
/*     */   
/*     */   public static Metadata encodeAsMetadata(ICalTimeZone tz) {
/* 168 */     Metadata meta = new Metadata();
/* 169 */     String tzid = tz.getID();
/* 170 */     meta.put("tzid", tzid);
/*     */     
/* 172 */     if (ICalTimeZone.lookupByTZID(tzid) != null) {
/* 173 */       return meta;
/*     */     }
/* 175 */     meta.put("so", tz.getStandardOffset());
/* 176 */     meta.put("d2ss", tz.getStandardDtStart());
/* 177 */     meta.put("d2sr", tz.getStandardRule());
/* 178 */     meta.put("sn", tz.getStandardTzname());
/*     */     
/* 180 */     meta.put("do", tz.getDaylightOffset());
/* 181 */     meta.put("s2ds", tz.getDaylightDtStart());
/* 182 */     meta.put("s2dr", tz.getDaylightRule());
/* 183 */     meta.put("dn", tz.getDaylightTzname());
/* 184 */     return meta;
/*     */   }
/*     */   
/*     */   public static ICalTimeZone decodeTimeZoneFromMetadata(Metadata m) throws ServiceException {
/*     */     String tzid;
/* 189 */     if (m.containsKey("tzid")) {
/* 190 */       String tzid = m.get("tzid");
/* 191 */       boolean hasDef = m.containsKey("so");
/* 192 */       if ((!DebugConfig.disableCalendarTZMatchByID) || (!hasDef)) {
/* 193 */         ICalTimeZone tz = WellKnownTimeZones.getTimeZoneById(tzid);
/* 194 */         if (tz != null)
/* 195 */           return tz;
/* 196 */         if (!hasDef) {
/* 197 */           ZimbraLog.calendar.debug("Unknown time zone \"" + tzid + "\" in metadata; using UTC instead");
/* 198 */           return ICalTimeZone.getUTC().cloneWithNewTZID(tzid);
/*     */         }
/*     */       }
/*     */     } else {
/* 202 */       tzid = "unknown time zone"; }
/* 203 */     ICalTimeZone newTz = newICalTimeZone(tzid, m);
/* 204 */     ICalTimeZone tz = ICalTimeZone.lookupByRule(newTz, false);
/* 205 */     return tz;
/*     */   }
/*     */   
/*     */   private static ICalTimeZone newICalTimeZone(String tzId, Metadata meta) throws ServiceException {
/* 209 */     int standardOffset = (int)meta.getLong("so", 0L);
/* 210 */     String dayToStdDtStart = meta.get("d2ss", null);
/* 211 */     String dayToStdRule = meta.get("d2sr", null);
/* 212 */     String standardTzname = meta.get("sn", null);
/*     */     
/* 214 */     int daylightOffset = (int)meta.getLong("do", 0L);
/* 215 */     String stdToDayDtStart = meta.get("s2ds", "16010101T000000");
/* 216 */     String stdToDayRule = meta.get("s2dr", null);
/* 217 */     String daylightTzname = meta.get("dn", null);
/*     */     
/* 219 */     ICalTimeZone tz = new ICalTimeZone(tzId, standardOffset, dayToStdDtStart, dayToStdRule, standardTzname, daylightOffset, stdToDayDtStart, stdToDayRule, daylightTzname);
/*     */     
/* 221 */     tz.initFromICalData(true);
/* 222 */     return tz;
/*     */   }
/*     */   
/*     */   public static Metadata encodeAsMetadata(TimeZoneMap tzmap) {
/* 226 */     Metadata meta = new Metadata();
/* 227 */     Map<String, Integer> tzIndex = new HashMap();
/* 228 */     int nextIndex = 0;
/* 229 */     for (Iterator<Map.Entry<String, ICalTimeZone>> iter = tzmap.getMap().entrySet().iterator(); iter.hasNext();) {
/* 230 */       Map.Entry<String, ICalTimeZone> entry = (Map.Entry)iter.next();
/* 231 */       String tzid = (String)entry.getKey();
/* 232 */       if ((tzid != null) && (tzid.length() >= 1))
/*     */       {
/* 234 */         ICalTimeZone zone = (ICalTimeZone)entry.getValue();
/* 235 */         String realTzid = zone.getID();
/* 236 */         if (!tzIndex.containsKey(realTzid)) {
/* 237 */           meta.put("#" + nextIndex, encodeAsMetadata(zone));
/* 238 */           tzIndex.put(realTzid, Integer.valueOf(nextIndex));
/* 239 */           nextIndex++;
/*     */         }
/*     */       } }
/* 242 */     for (Iterator<Map.Entry<String, String>> iter = tzmap.getAliasMap().entrySet().iterator(); iter.hasNext();) {
/* 243 */       Map.Entry<String, String> entry = (Map.Entry)iter.next();
/* 244 */       String alias = (String)entry.getKey();
/* 245 */       String realTzid = (String)entry.getValue();
/* 246 */       if (tzIndex.containsKey(realTzid)) {
/* 247 */         int index = ((Integer)tzIndex.get(realTzid)).intValue();
/* 248 */         meta.put(alias, index);
/*     */       }
/*     */     }
/* 251 */     return meta;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static TimeZoneMap decodeFromMetadata(Metadata meta, ICalTimeZone localTZ)
/*     */     throws ServiceException
/*     */   {
/* 262 */     Map<String, ?> map = meta.asMap();
/* 263 */     Map<String, String> aliasMap = new HashMap();
/* 264 */     ICalTimeZone[] tzlist = new ICalTimeZone[map.size()];
/*     */     
/* 266 */     for (Map.Entry<String, ?> entry : map.entrySet()) {
/* 267 */       String key = (String)entry.getKey();
/* 268 */       if ((key != null) && (key.length() > 0) && 
/* 269 */         (key.charAt(0) == '#')) {
/* 270 */         int idx = Integer.parseInt(key.substring(1));
/* 271 */         Metadata tzMeta = (Metadata)entry.getValue();
/* 272 */         String tzidMeta = tzMeta.get("tzid", null);
/* 273 */         if (tzidMeta != null) {
/* 274 */           ICalTimeZone tz = decodeTimeZoneFromMetadata(tzMeta);
/* 275 */           if (tz != null) {
/* 276 */             String tzid = tz.getID();
/* 277 */             if (!DebugConfig.disableCalendarTZMatchByID)
/* 278 */               tzid = TZIDMapper.canonicalize(tzid);
/* 279 */             if (!tzidMeta.equals(tzid)) {
/* 280 */               aliasMap.put(tzidMeta, tzid);
/* 281 */               tz = WellKnownTimeZones.getTimeZoneById(tzid);
/*     */             }
/* 283 */             tzlist[idx] = tz;
/*     */           }
/*     */         }
/*     */       }
/*     */     }
/*     */     
/*     */ 
/* 290 */     Map<String, ICalTimeZone> tzmap = new HashMap();
/* 291 */     for (ICalTimeZone tz : tzlist) {
/* 292 */       if (tz != null) {
/* 293 */         tzmap.put(tz.getID(), tz);
/*     */       }
/*     */     }
/* 296 */     for (Map.Entry<String, ?> entry : map.entrySet()) {
/* 297 */       String tzid = (String)entry.getKey();
/* 298 */       if ((tzid != null) && (tzid.length() > 0) && 
/* 299 */         (tzid.charAt(0) != '#')) {
/* 300 */         int idx = -1;
/*     */         try {
/* 302 */           idx = Integer.parseInt(entry.getValue().toString());
/*     */         } catch (NumberFormatException e) {}
/* 304 */         if ((idx >= 0) && (idx < tzlist.length)) {
/* 305 */           ICalTimeZone tz = tzlist[idx];
/* 306 */           if (tz != null) {
/* 307 */             String realId = tz.getID();
/* 308 */             if (!realId.equals(tzid)) {
/* 309 */               aliasMap.put(tzid, realId);
/*     */             }
/*     */           }
/*     */         }
/*     */       }
/*     */     }
/*     */     
/* 316 */     return new TimeZoneMap(tzmap, aliasMap, localTZ);
/*     */   }
/*     */   
/*     */   public static Metadata encodeMetadata(Attach att) {
/* 320 */     Metadata meta = new Metadata();
/* 321 */     if (att.getUri() != null) {
/* 322 */       meta.put("uri", att.getUri());
/* 323 */       meta.put("ct", att.getContentType());
/*     */     } else {
/* 325 */       meta.put("bin", att.getBinaryB64Data());
/*     */     }
/* 327 */     return meta;
/*     */   }
/*     */   
/*     */   public static Attach decodeAttachFromMetadata(Metadata meta) {
/* 331 */     String uri = meta.get("uri", null);
/* 332 */     String ct = meta.get("ct", null);
/* 333 */     if (uri != null) {
/* 334 */       return Attach.fromUriAndContentType(uri, ct);
/*     */     }
/* 336 */     String binary = meta.get("bin", null);
/* 337 */     return Attach.fromEncodedAndContentType(binary, ct);
/*     */   }
/*     */   
/*     */   public static Geo decodeGeoFromMetadata(Metadata meta)
/*     */   {
/* 342 */     String lat = meta.get("lat", "0");
/* 343 */     String lon = meta.get("lon", "0");
/* 344 */     return new Geo(lat, lon);
/*     */   }
/*     */   
/*     */   public static Metadata encodeMetadata(Geo geo)
/*     */   {
/* 349 */     Metadata meta = new Metadata();
/* 350 */     meta.put("lat", geo.getLatitude());
/* 351 */     meta.put("lon", geo.getLongitude());
/* 352 */     return meta;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/mailbox/calendar/Util.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */